(() => {

    'use strict';
    goog.provide('Blockly.Arduino.ruilong');
    goog.require('Blockly.Arduino');

    

    Blockly.Arduino.forBlock['ruilong_magnetic'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var code = "";
        if (window.isNaN(dropdown_pin)) {
            var funcName = 'mixly_digitalRead';
            var code2 = 'int' + ' ' + funcName + '(uint8_t pin) {\n'
            + '  pinMode(pin, INPUT);\n'
            + '  return digitalRead(pin);\n'
            + '}\n';
            Blockly.Arduino.definitions_[funcName] = code2;
            code = 'mixly_digitalRead(' + dropdown_pin + ')';
        } else {
            if (Blockly.Arduino.setups_['setup_output_' + dropdown_pin]) {
                //存在pinMode已设为output则不再设为input
            } else {
                Blockly.Arduino.setups_['setup_input_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);';
            }
            code = 'digitalRead(' + dropdown_pin + ')';
        }
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock.ruilong_pir = Blockly.Arduino.forBlock.ruilong_magnetic;
    Blockly.Arduino.forBlock.ruilong_vibration = Blockly.Arduino.forBlock.ruilong_magnetic;
    Blockly.Arduino.forBlock.ruilong_tilt = Blockly.Arduino.forBlock.ruilong_magnetic;
    Blockly.Arduino.forBlock.ruilong_touch = Blockly.Arduino.forBlock.ruilong_magnetic;
    Blockly.Arduino.forBlock.ruilong_collision = Blockly.Arduino.forBlock.ruilong_magnetic;
    Blockly.Arduino.forBlock.ruilong_button = Blockly.Arduino.forBlock.ruilong_magnetic;
    Blockly.Arduino.forBlock.ruilong_obstacle = Blockly.Arduino.forBlock.ruilong_magnetic;
    Blockly.Arduino.forBlock.ruilong_switch = Blockly.Arduino.forBlock.ruilong_magnetic;


    Blockly.Arduino.forBlock['ruilong_hc_sr04'] = function () {
        var dropdown_pin1 = Blockly.Arduino.valueToCode(this, 'PIN1', Blockly.Arduino.ORDER_ATOMIC);
        var dropdown_pin2 = Blockly.Arduino.valueToCode(this, 'PIN2', Blockly.Arduino.ORDER_ATOMIC);
        Blockly.Arduino.setups_['setup_output_' + dropdown_pin1] = 'pinMode(' + dropdown_pin1 + ', OUTPUT);';
        Blockly.Arduino.setups_['setup_output_' + dropdown_pin2] = 'pinMode(' + dropdown_pin2 + ', INPUT);\n';
        var funcName = 'checkdistance_' + dropdown_pin1 + '_' + dropdown_pin2;
        var code = 'float' + ' ' + funcName + '() {\n'
        + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n' + '  delayMicroseconds(2);\n'
        + '  digitalWrite(' + dropdown_pin1 + ', HIGH);\n' + '  delayMicroseconds(10);\n'
        + '  digitalWrite(' + dropdown_pin1 + ', LOW);\n'
        + '  float distance = pulseIn(' + dropdown_pin2 + ', HIGH) / 58.00;\n'
        + '  delay(10);\n' + '  return distance;\n'
        + '}\n';
        Blockly.Arduino.definitions_[funcName] = code;
        return [funcName + '()', Blockly.Arduino.ORDER_ATOMIC];
    }
    Blockly.Arduino.forBlock['ruilong_hc_sr04_3p'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        //Blockly.Arduino.setups_['setup_output_' + dropdown_pin] = 'pinMode(' + dropdown_pin + ', INPUT);\n';
        var funcName = 'checkdistance_' + dropdown_pin;
        var code = 'float' + ' ' + funcName + '() {\n'
        + '   pinMode(' + dropdown_pin + ', OUTPUT);\n'
        + '  digitalWrite(' + dropdown_pin + ', LOW);\n' + '  delayMicroseconds(2);\n'
        + '  digitalWrite(' + dropdown_pin + ', HIGH);\n' + '  delayMicroseconds(10);\n'
        + '  digitalWrite(' + dropdown_pin + ', LOW);\n'
        + '   pinMode(' + dropdown_pin + ', INPUT);\n'
        + '  float distance = pulseIn(' + dropdown_pin + ', HIGH) / 58.00;\n'
        + '  delay(10);\n' + '  return distance;\n'
        + '}\n';
        Blockly.Arduino.definitions_[funcName] = code;
        return [funcName + '()', Blockly.Arduino.ORDER_ATOMIC];
    }
    Blockly.Arduino.forBlock['ruilong_dht11'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var what = this.getFieldValue('WHAT');
        Blockly.Arduino.definitions_['define_dht11'] = '#include <dht11.h>';
        Blockly.Arduino.definitions_['var_dht11' + dropdown_pin] = 'dht11 myDHT_' + dropdown_pin + ';';
        var funcName = 'dht_' + dropdown_pin + '_get' + what;
        var code = 'int' + ' ' + funcName + '() {\n'
        + '  int chk = myDHT_' + dropdown_pin + '.read(' + dropdown_pin + ');\n'
        + '  int value = myDHT_' + dropdown_pin + '.' + what + ';\n'
        + '  return value;\n'
        + '}\n';
        Blockly.Arduino.definitions_[funcName] = code;
        return [funcName + '()', Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_MRP121'] = function () {

        var touch = this.getFieldValue('MRP121_TOUCH');
        var touch_state = this.getFieldValue('MRP121_TOUCH_STATE');
        var branch = Blockly.Arduino.statementToCode(this, 'DO');
        Blockly.Arduino.definitions_['define_mpr121_1'] = '#include <Wire.h>\n#include "Adafruit_MPR121.h"\n';
        Blockly.Arduino.definitions_['define_mpr121_2'] = '#ifndef _BV\n#define _BV(bit) (1 << (bit)) \n#endif\n';
        Blockly.Arduino.definitions_['define_mpr121_3'] = 'Adafruit_MPR121 cap = Adafruit_MPR121();';

        Blockly.Arduino.setups_['setup_mpr121'] = ' cap.begin(0x5A);';

        var code;
        if (touch_state == 0) {
            code = 'if(!(cap.touched()&_BV(' + touch + ')))\n';
            code += '{\n' + branch + '\n}';
        } else if (touch_state == 1) {
            code = 'if(cap.touched()&_BV(' + touch + '))\n';
            code += '{\n' + branch + '\n}';
        }

        return code;
    };

    Blockly.Arduino.forBlock['ruilong_TCA9548A'] = function () {

        var ch = this.getFieldValue('TCA9548A_CH');
        var branch = Blockly.Arduino.statementToCode(this, 'DO');
        Blockly.Arduino.definitions_['define_RL_TCA9548A_h'] = '#include <RL_TCA9548A.h>\n';

        Blockly.Arduino.definitions_['define_TCA9548A_I2CMultiplexer'] = 'TCA9548A_I2CMultiplexer I2CMultiplexer(0x70);';



        var code;

        code = 'I2CMultiplexer.selectPort(' + ch + ');\n'
        + branch + '\n';

        return code;
    };

    Blockly.Arduino.forBlock['ruilong_ds18B20'] = function () {
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var unit = this.getFieldValue('UNIT');
        Blockly.Arduino.definitions_['include_OneWire'] = '#include <OneWire.h>';
        Blockly.Arduino.definitions_['include_DallasTemperature'] = '#include <DallasTemperature.h>';
        Blockly.Arduino.definitions_['var_OneWire_oneWire_' + dropdown_pin] = 'OneWire oneWire_' + dropdown_pin + '(' + dropdown_pin + ');';
        Blockly.Arduino.definitions_['var_DallasTemperature_sensors_' + dropdown_pin] = 'DallasTemperature sensors_' + dropdown_pin + '(&oneWire_' + dropdown_pin + ');';
        Blockly.Arduino.definitions_['var_DeviceAddress_insideThermometer'] = 'DeviceAddress insideThermometer;';
        Blockly.Arduino.setups_['setup_sensors_' + dropdown_pin + '_getAddress'] = 'sensors_' + dropdown_pin + '.getAddress(insideThermometer, 0);';
        Blockly.Arduino.setups_['setup_sensors_' + dropdown_pin + '_setResolution'] = 'sensors_' + dropdown_pin + '.setResolution(insideThermometer, 9);';
        var funcName = 'ds18b20_' + dropdown_pin + '_getTemp';
        var code = 'float' + ' ' + funcName + '(int w) {\n'
        + '  sensors_' + dropdown_pin + '.requestTemperatures();\n'
        + '  if(w==0) {\nreturn sensors_' + dropdown_pin + '.getTempC(insideThermometer);\n}\n'
        + '  else {\nreturn sensors_' + dropdown_pin + '.getTempF(insideThermometer);\n}\n'
        + '}\n';
        Blockly.Arduino.definitions_[funcName] = code;
        return ['ds18b20_' + dropdown_pin + '_getTemp(' + unit + ')', Blockly.Arduino.ORDER_ATOMIC];
    };
    /*
    Blockly.Arduino.forBlock['ruilong_GP2Y1026'] = function () {
      var variable = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
      var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN',Blockly.Arduino.ORDER_ATOMIC);
        var branch = Blockly.Arduino.statementToCode(this, 'DO');
      Blockly.Arduino.definitions_['include_SoftwareSerial'] = '#include <SoftwareSerial.h>';
      Blockly.Arduino.definitions_['mySerial'] = 'SoftwareSerial mySerial('+dropdown_pin'+, 11);';
      Blockly.Arduino.definitions_['rx_data'] = 'unsigned char RX_Data;//接收数据\n'
                                                += 'unsigned char flag_start,flag_stop,flag_analysis; //接收标志位\n'
                                                += 'unsigned char BUF[7];//接收缓存\n'
                                                += 'unsigned char index;//接收计数\n';
    
        Blockly.Arduino.setups_['setup_mySerial'] = ' mySerial.begin(2400);\n delay(1000);\n';
      
      
    
      var code='';
    
      return code;
        
    };*/
    //SHT20温湿度传感器
    Blockly.Arduino.forBlock['ruilong_SHT20'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_DFRobot_SHT20'] = '#include <DFRobot_SHT20.h>';
        Blockly.Arduino.definitions_['var_declare_DFRobot_SHT20'] = 'DFRobot_SHT20 sht20;\n';
        Blockly.Arduino.setups_['setup_sht20initSHT20'] = 'sht20.initSHT20();';
        Blockly.Arduino.setups_['setup_sht20.checkSHT20'] = 'sht20.checkSHT20(); \n';
        var dropdown_type = this.getFieldValue('SHT20_TYPE');
        var code = dropdown_type;
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    //AHT20/21温湿度传感器
    Blockly.Arduino.forBlock['ruilong_AHT20_21'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_RL_AHT21'] = '#include <RL_AHTxx.h>';
        Blockly.Arduino.definitions_['AHT21Class_AHT21'] = 'AHTxx aht20(AHTXX_ADDRESS_X38, AHT2x_SENSOR);\n';
        Blockly.Arduino.setups_['AHT21_begin'] = 'aht20.begin();\n';

        var dropdown_type = this.getFieldValue('AHT21_TYPE');
        var code = dropdown_type;
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    //VK36N16I 触摸键盘模块
    Blockly.Arduino.forBlock['ruilong_16CHTOUCH'] = function () {

        var key = this.getFieldValue('KEY_ID');

        Blockly.Arduino.definitions_['include_RL_VK36N16I'] = '#include "RL_VK36N16I.h"';
        Blockly.Arduino.definitions_['VK36N16I_TOUCH_touch'] = 'VK36N16I_TOUCH touch;';


        Blockly.Arduino.setups_['setup_touch_begin'] = 'touch.begin();\n';

        var code = 'touch.touchRead('+key+')';

        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    //VK36N16I 16键触摸键盘模块 方向+功能键
    Blockly.Arduino.forBlock['ruilong_16CHTOUCH_KJ']=Blockly.Arduino.forBlock['ruilong_16CHTOUCH'];
     //BH1750数字环境光传感器
    Blockly.Arduino.forBlock['ruilong_BH1750_INIT'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>\n';
        Blockly.Arduino.definitions_['include_RL_BH1750'] = '#include <RL_BH1750.h>\n';
        Blockly.Arduino.definitions_['var_BH1750_lightMeter'] = 'BH1750 lightMeter;\n';
        Blockly.Arduino.setups_['lightMeter_begin'] = 'Wire.begin();\n  lightMeter.begin();\n';
        return "";
    };

    Blockly.Arduino.forBlock['ruilong_BH1750_SET'] = function () {

        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>\n';
        Blockly.Arduino.definitions_['include_RL_BH1750'] = '#include <RL_BH1750.h>\n';
        Blockly.Arduino.definitions_['var_BH1750_lightMeter'] = 'BH1750 lightMeter;\n';
        Blockly.Arduino.setups_['lightMeter_begin'] = 'Wire.begin();\n  lightMeter.begin();\n';

        var code='lightMeter.configure(BH1750::'+ this.getFieldValue('MODE')+');\n';
        return code;

    };

    Blockly.Arduino.forBlock['ruilong_BH1750_SETMTREG'] = function () {

        var time = Blockly.Arduino.valueToCode(this, 'TIME', Blockly.Arduino.ORDER_ATOMIC);

        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>\n';
        Blockly.Arduino.definitions_['include_RL_BH1750'] = '#include <RL_BH1750.h>\n';
        Blockly.Arduino.definitions_['var_BH1750_lightMeter'] = 'BH1750 lightMeter;\n';
        Blockly.Arduino.setups_['lightMeter_begin'] = 'Wire.begin();\n  lightMeter.begin();\n';

        var code='lightMeter.setMTreg('+ time +');\n';
        return code;

    };

    Blockly.Arduino.forBlock['ruilong_BH1750_MTRADY'] = function () {

        var time = Blockly.Arduino.valueToCode(this, 'TIME', Blockly.Arduino.ORDER_ATOMIC);

        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>\n';
        Blockly.Arduino.definitions_['include_RL_BH1750'] = '#include <RL_BH1750.h>\n';
        Blockly.Arduino.definitions_['var_BH1750_lightMeter'] = 'BH1750 lightMeter;\n';
        Blockly.Arduino.setups_['lightMeter_begin'] = 'Wire.begin();\n  lightMeter.begin();\n';

        var code = 'lightMeter.measurementReady(true)';
        return [code, Blockly.Arduino.ORDER_ATOMIC];

    };
    Blockly.Arduino.forBlock['ruilong_BH1750'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>\n';
        Blockly.Arduino.definitions_['include_RL_BH1750'] = '#include <RL_BH1750.h>\n';
        Blockly.Arduino.definitions_['var_BH1750_lightMeter'] = 'BH1750 lightMeter;\n';
        Blockly.Arduino.setups_['lightMeter_begin'] = 'Wire.begin();\n  lightMeter.begin();';
        var code = 'lightMeter.readLightLevel()';
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    //LSM6DSM 6轴加速度陀螺仪传感器
    Blockly.Arduino.forBlock['ruilong_LSM6DSM'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        
        Blockly.Arduino.definitions_['include_RL_LSM6DSM'] = '#include "RL_LSM6DSM.h"';
        Blockly.Arduino.definitions_['var_LSM6DSM_AccGyro'] = 'LSM6DSM AccGyro;\n';
        Blockly.Arduino.setups_['setup_AccGyro_begin'] = 'AccGyro.begin(0x6B);';
        var code = 'AccGyro.'+ this.getFieldValue('LSM6DSM') +'()';
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    //MSA311 3轴加速度传感器
    Blockly.Arduino.forBlock['ruilong_MSA311_READ'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_Adafruit_MSA301'] = '#include <Adafruit_MSA301.h>';
        Blockly.Arduino.definitions_['include_Adafruit_Senso'] = '#include <Adafruit_Sensor.h>';
        Blockly.Arduino.definitions_['var_Adafruit_MSA311_msa'] = 'Adafruit_MSA311 msa;\n';
        Blockly.Arduino.setups_['setup_msa_begin'] = 'msa.begin();';
        var code = "msa.read();\n";
        return code;
    };
    Blockly.Arduino.forBlock['ruilong_MSA311'] = function () {

        var code = "msa."+ this.getFieldValue('MSA311');+"\n";
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    //BMP280温度气压传感器
    Blockly.Arduino.forBlock['ruilong_BMP280'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_BMP280'] = '#include "BMP280.h"';
        Blockly.Arduino.definitions_['var_declare_BMP280'] = 'BMP280 bmp;\n';
        Blockly.Arduino.setups_['setup_BMP280'] = 'bmp.begin();';

        var dropdown_type = this.getFieldValue('BMP280_TYPE');
        var code = dropdown_type;
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    //BMP280温度气压传感器
    Blockly.Arduino.forBlock['ruilong_H203B'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_HP203B'] = '#include "HP203B.h"';
        Blockly.Arduino.definitions_['var_declare_HP203B'] = 'HP203B myHP203;\n';
        Blockly.Arduino.setups_['myHP203_begin'] = 'myHP203.begin();';

        var dropdown_type = this.getFieldValue('H203B_TYPE');
        var code = dropdown_type;
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };
    //传感器_重力感应块
    Blockly.Arduino.forBlock['ruilong_ADXL345'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_I2Cdev'] = '#include <I2Cdev.h>';
        Blockly.Arduino.definitions_['include_ADXL345'] = '#include <ADXL345.h>';
        Blockly.Arduino.definitions_['var_declare_ADXL345'] = 'ADXL345 accel;\n';
        Blockly.Arduino.setups_['setup_Wire.begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_accel.begin'] = 'accel.initialize(); \n';
        var dropdown_type = this.getFieldValue('ADXL345_PIN');
        var code = dropdown_type;
        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    //传感器_重力感应块
    Blockly.Arduino.forBlock['ruilong_ADXL345_setOffset'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_I2Cdev'] = '#include <I2Cdev.h>';
        Blockly.Arduino.definitions_['include_ADXL345'] = '#include <ADXL345.h>';
        Blockly.Arduino.definitions_['var_declare_ADXL345'] = 'ADXL345 accel;\n';
        Blockly.Arduino.setups_['setup_Wire.begin'] = 'Wire.begin();';
        Blockly.Arduino.setups_['setup_accel.begin'] = 'accel.initialize(); \n';

        var dropdown_type = this.getFieldValue('MIXEPI_ADXL345_OFFSET');
        var offset_value = Blockly.Arduino.valueToCode(this, 'OFFSET', Blockly.Arduino.ORDER_ATOMIC);
        var code;

        if (dropdown_type == "setOffsetX") {
            code = 'accel.setOffsetX(round(' + offset_value + '*4/15.9));\n';
        } else if (dropdown_type == "setOffsetY") {
            code = 'accel.setOffsetY(round(' + offset_value + '*4/15.9));\n';
        } else if (dropdown_type == "setOffsetZ") {
            code = 'accel.setOffsetZ(round(' + offset_value + '*4/15.9));\n';
        }

        return code;
    };
    //TCS34725颜色识别-获取数据
    Blockly.Arduino.forBlock['ruilong_TCS34725_Get_RGB'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_TCS34725'] = '#include <RL_TCS34725.h>';
        Blockly.Arduino.definitions_['var_TCS34725'] = 'TCS34725 tcs = TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);';
        Blockly.Arduino.definitions_['var_declare_RGB'] = 'float red, green, blue;';
        Blockly.Arduino.setups_['setup_TCS34725'] = 'if (tcs.begin()) {\nSerial.println("Found sensor");\n} \nelse { \nSerial.println("No TCS34725 found ... check your connections");\nwhile (1);\n}';
        var code = "delay(60);\ntcs.getRGB(&red, &green, &blue);\n";
        var RGB = this.getFieldValue('TCS34725_GETRGB');
        return [RGB, Blockly.Arduino.ORDER_ATOMIC];
    };
    //VEML6040颜色识别-获取数据
    Blockly.Arduino.forBlock['ruilong_VEML6040_Get_RGBW'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_RL_VEML6040'] = '#include "RL_VEML6040.h"';
        Blockly.Arduino.definitions_['var_VEML6040_RGBWSensor'] = 'VEML6040 RGBWSensor;';
        Blockly.Arduino.setups_['setup_VEML6040'] = 'Wire.begin();\n  RGBWSensor.begin();\n  RGBWSensor.setConfiguration(VEML6040_IT_320MS + VEML6040_AF_AUTO + VEML6040_SD_ENABLE);\n';
        
        var RGB = this.getFieldValue('VEML6040_GETRGBW');
        return [RGB, Blockly.Arduino.ORDER_ATOMIC];
    };


    //MAX30105心率血氧传感器
    Blockly.Arduino.forBlock['ruilong_MAX3010X_Value'] = function () {
        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_MAX30105'] = '#include "MAX30105.h"';
        Blockly.Arduino.definitions_['MAX30105_particleSensor'] = 'MAX30105 particleSensor;';


        Blockly.Arduino.setups_['setup_particleSensor_begin'] = 'particleSensor.begin();';
        Blockly.Arduino.setups_['setup_particleSensor_setup'] = 'particleSensor.setup();';

        var dropdown_type = this.getFieldValue('MAX3010X_TYPE');
        var code = dropdown_type;

        return [code, Blockly.Arduino.ORDER_ATOMIC];
    };

    Blockly.Arduino.forBlock['ruilong_MAX3010X_HR'] = function () {
        var variable = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
        var branch = Blockly.Arduino.statementToCode(this, 'DO');

        Blockly.Arduino.definitions_['include_Wire'] = '#include <Wire.h>';
        Blockly.Arduino.definitions_['include_MAX30105'] = '#include "MAX30105.h"'
        Blockly.Arduino.definitions_['include_heartRate'] = '#include "heartRate.h"'

        Blockly.Arduino.definitions_['MAX30105_particleSensor'] = 'MAX30105 particleSensor;';
        Blockly.Arduino.definitions_['MAX30105_var'] = 'const byte RATE_SIZE = 4; //Increase this for more averaging. 4 is good.\n'
        + ' byte rates[RATE_SIZE]; //Array of heart rates\n'
        + ' byte rateSpot = 0;\n'
        + ' long lastBeat = 0; //Time at which the last beat occurred\n'
        + ' float beatsPerMinute;\n'
        + ' int ' + variable + ';\n';

        Blockly.Arduino.setups_['setup_MAX30105'] = 'particleSensor.begin(Wire, I2C_SPEED_FAST);\n'
        + '  particleSensor.setup(); //Configure sensor with default settings\n'
        + '  particleSensor.setPulseAmplitudeRed(0x0A); //Turn Red LED to low to indicate sensor is running\n'
        + '  particleSensor.setPulseAmplitudeGreen(0); //Turn off Green LED\n';
        var code = 'long irValue = particleSensor.getIR();\n'
        + 'if (checkForBeat(irValue) == true)\n'
        + '  {\n'
        + '    //We sensed a beat!\n'
        + '    long delta = millis() - lastBeat;\n'
        + '    lastBeat = millis();\n'

        + '    beatsPerMinute = 60 / (delta / 1000.0);\n'

        + '    if (beatsPerMinute < 255 && beatsPerMinute > 20)\n'
        + '    {\n'
        + '      rates[rateSpot++] = (byte)beatsPerMinute; //Store this reading in the array\n'
        + '      rateSpot %= RATE_SIZE; //Wrap variable\n'

        + '      //Take average of readings\n'
        + variable + ' = 0;\n'
        + '      for (byte x = 0 ; x < RATE_SIZE ; x++)\n'
        + variable + ' += rates[x];\n'
        + variable + ' /= RATE_SIZE;\n'
        + '    }\n'
        + '  }\n'
        + branch;

        return code;
    };


    Blockly.Arduino.forBlock['ruilong_GP2Y1026'] = function () {
        var variable = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'), Blockly.Variables.NAME_TYPE);
        //Blockly.Arduino.definitions_['var_declare' + variable] = 'long ' + variable + ';';
        var dropdown_pin = Blockly.Arduino.valueToCode(this, 'PIN', Blockly.Arduino.ORDER_ATOMIC);
        var branch = Blockly.Arduino.statementToCode(this, 'DO');
        // var branch2 = Blockly.Arduino.statementToCode(this, 'DO2');
        //var varName = Blockly.Arduino.variableDB_.getName(this.getFieldValue('VAR'),Blockly.Variables.NAME_TYPE);

        Blockly.Arduino.definitions_['include_SoftwareSerial'] = '#include <HardwareSerial.h>\n ';

        Blockly.Arduino.definitions_['rx_data'] = 'unsigned char RX_Data;//接收数据\n'
        + 'unsigned char flag_start,flag_stop,flag_analysis; //接收标志位\n'
        + 'unsigned char BUF[7];//接收缓存\n'
        + 'unsigned char index_;//接收计数\n'
        + 'unsigned int ' + variable + ';\n';
        Blockly.Arduino.setups_['setup_Serial1'] = 'Serial1.begin(2400,SERIAL_8N1,13,-1);\n'
        + '  delay(1000);\n';

        var code = 'if ( Serial1.available()>0){  \n'
        + ' RX_Data=Serial1.read();\n'
        + 'if(RX_Data==0xAA) //判断帧头\n'
        + '  flag_start=1; //帧头1标志位置位\n'
        + ' if(flag_start) //当帧头1标志位置位后\n'
        + '  {\n'
        + '    if(flag_analysis==0) //判断帧头2 且数据分析开始\n'
        + '    {\n'
        + '     flag_start=1; //帧头2标志位置位\n'
        + '    flag_stop=0;//停止位标志位清零 \n'
        + '    }\n'
        + '   if(flag_start) //当帧头2标志位置位后\n'
        + '   {\n'
        + '    BUF[index_++]=RX_Data;//依次传输数据\n'
        + '    if(RX_Data==0xff)//判断后\n'
        + '   {  \n'
        + '      flag_start=0;//帧头1标志位清零\n'
        + '      flag_stop=1;//停止位标志位置位\n'
        + '       flag_analysis=1; \n'
        + '       index_=0;\n'
        + '   }\n'
        + '   }\n'
        + ' }\n'
        + ' if(flag_analysis==1) //提取到每一帧数据，进行判断控制\n'
        + ' {\n'
        + '   if((BUF[1]+BUF[2]+BUF[3]+BUF[4])%256==BUF[5]){\n'
        + '   unsigned int Vout=BUF[1]<<8|BUF[2];\n'
        + '   unsigned int Vref=BUF[3]<<8|BUF[4];\n'
        + '   //float Vout_mv=Vout / 1024.0 * 5000.0;\n'
        + '   float a=Vout*1.395; /// 1024.0 * 5.0*100.0 / 0.35;\n'
        + variable + '=(int)a;\n'
        + branch
        + '    }\n'
        + '    flag_analysis=0; //分析处理结束\n'
        + ' }\n'
        + ' } '

        return code;
    };


    Blockly.Arduino.forBlock['ruilong_PCF8574T_READ'] = function() {
     var ch = this.getFieldValue('CH');


     Blockly.Arduino.definitions_['_#include_RL_PCF8574T_h_'] =  '#include "RL_PCF8574T.h" ';
     Blockly.Arduino.definitions_['_PCF8574_pcf8574(0x20)_'] =  'PCF8574 pcf8574(0x27);//默认I2C地址为0X27';

     Blockly.Arduino.setups_['pcf8574_pinMode'] = 'pcf8574.begin();\n';
     Blockly.Arduino.setups_['pcf8574_pinMode'+ch] = 'pcf8574.pinMode(P'+ ch +', INPUT);\n';

     var code = 'pcf8574.digitalRead(P'+ch+')';
     return [code, Blockly.Arduino.ORDER_ATOMIC];
 };   

 Blockly.Arduino.forBlock['ruilong_PCF8574T_WRITE'] = function() {
     var ch = this.getFieldValue('CH');
     var dropdown_stat = Blockly.Arduino.valueToCode(this, 'STAT', Blockly.Arduino.ORDER_ATOMIC);

     Blockly.Arduino.definitions_['_#include_RL_PCF8574T_h_'] =  '#include "RL_PCF8574T.h" ';
     Blockly.Arduino.definitions_['_PCF8574_pcf8574(0x20)_'] =  'PCF8574 pcf8574(0x27);//默认I2C地址为0X27';

     Blockly.Arduino.setups_['pcf8574_pinMode'] = 'pcf8574.begin();\n';
     Blockly.Arduino.setups_['pcf8574_pinMode'+ch] = 'pcf8574.pinMode(P'+ ch +', OUTPUT);\n';

     var code = 'pcf8574.digitalWrite(P'+ch+',' + dropdown_stat + ');';
     return code;
 }; 

Blockly.Arduino.forBlock['ruilong_FPM383_ED_ID'] = function() {
      var dropdown_rxpin = Blockly.Arduino.valueToCode(this, 'RXPIN',Blockly.Arduino.ORDER_ATOMIC);
      var dropdown_txpin = Blockly.Arduino.valueToCode(this, 'TXPIN',Blockly.Arduino.ORDER_ATOMIC);

      Blockly.Arduino.definitions_['include_RL_FPM383'] = '#include "RL_FPM383.h"';
      Blockly.Arduino.definitions_['FPM383_pms'] = 'FPM383 fpm('+dropdown_rxpin+','+dropdown_txpin+');//RX TX';
      Blockly.Arduino.setups_['setup_fpm_begin'] = 'fpm.begin(57600);\n  fpm.getChipSN();\n  delay(200);';
      var mode = this.getFieldValue('MODE');
      var id = Blockly.Arduino.valueToCode(this, 'ID_NO.', Blockly.Arduino.ORDER_ATOMIC);

      var code = mode+"("+id+")";

      return [code, Blockly.Arduino.ORDER_ATOMIC];
      };
    Blockly.Arduino.forBlock['ruilong_FPM383_EMPTY'] = function() {
      var dropdown_rxpin = Blockly.Arduino.valueToCode(this, 'RXPIN',Blockly.Arduino.ORDER_ATOMIC);
      var dropdown_txpin = Blockly.Arduino.valueToCode(this, 'TXPIN',Blockly.Arduino.ORDER_ATOMIC);

      Blockly.Arduino.definitions_['include_RL_FPM383'] = '#include "RL_FPM383.h"';
      Blockly.Arduino.definitions_['FPM383_pms'] = 'FPM383 fpm('+dropdown_rxpin+','+dropdown_txpin+');//RX TX';
      Blockly.Arduino.setups_['setup_fpm_begin'] = 'fpm.begin(57600);\n  fpm.getChipSN();\n  delay(200);';
      var code = "fpm.empty()";

      return [code, Blockly.Arduino.ORDER_ATOMIC];
      };
    Blockly.Arduino.forBlock['ruilong_FPM383_IDENTIFY'] = function() {
      var dropdown_rxpin = Blockly.Arduino.valueToCode(this, 'RXPIN',Blockly.Arduino.ORDER_ATOMIC);
      var dropdown_txpin = Blockly.Arduino.valueToCode(this, 'TXPIN',Blockly.Arduino.ORDER_ATOMIC);

      Blockly.Arduino.definitions_['include_RL_FPM383'] = '#include "RL_FPM383.h"';
      Blockly.Arduino.definitions_['FPM383_pms'] = 'FPM383 fpm('+dropdown_rxpin+','+dropdown_txpin+');//RX TX';
      Blockly.Arduino.setups_['setup_fpm_begin'] = 'fpm.begin(57600);\n  fpm.getChipSN();\n  delay(200);';
      var code = "fpm.identify(true)";

      return [code, Blockly.Arduino.ORDER_ATOMIC];
      };
      Blockly.Arduino.forBlock['ruilong_FPM383_INQUIRY'] = function() {
      var dropdown_rxpin = Blockly.Arduino.valueToCode(this, 'RXPIN',Blockly.Arduino.ORDER_ATOMIC);
      var dropdown_txpin = Blockly.Arduino.valueToCode(this, 'TXPIN',Blockly.Arduino.ORDER_ATOMIC);

      Blockly.Arduino.definitions_['include_RL_FPM383'] = '#include "RL_FPM383.h"';
      Blockly.Arduino.definitions_['FPM383_pms'] = 'FPM383 fpm('+dropdown_rxpin+','+dropdown_txpin+');//RX TX';
      Blockly.Arduino.setups_['setup_fpm_begin'] = 'fpm.begin(57600);\n  fpm.getChipSN();\n  delay(200);';
      var code = "fpm.inquiry()";

      return [code, Blockly.Arduino.ORDER_ATOMIC];
      };
 
 Blockly.Arduino.forBlock['ruilong_RS485_FSFX_INIT'] = function() {
    var dropdown_rxpin = Blockly.Arduino.valueToCode(this, 'RXPIN',Blockly.Arduino.ORDER_ATOMIC);
    var dropdown_txpin = Blockly.Arduino.valueToCode(this, 'TXPIN',Blockly.Arduino.ORDER_ATOMIC);

    Blockly.Arduino.definitions_['include_RS485_FSFX'] = '#include "RL_RS485_FSFX.h"';
    Blockly.Arduino.definitions_['RS485_FSFX_rs485_fsfx'] = 'RS485_FSFX rs485_fsfx('+dropdown_rxpin+','+dropdown_txpin+');//RX TX';
    Blockly.Arduino.setups_['setup_rs485_fsfx_begin'] = 'rs485_fsfx.begin(4800);\n';

    return '';
};  

Blockly.Arduino.forBlock['ruilong_RS485_FSFX_REQUESTFS'] = function() {
  var code = "rs485_fsfx.requestfs();\n";
  return code;
};
Blockly.Arduino.forBlock['ruilong_RS485_FSFX_READFS'] = function() {
  var code = "rs485_fsfx.readfs()";
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.forBlock['ruilong_RS485_FSFX_READFS_LEVEL'] = function() {
  var code = "rs485_fsfx.readfs_level()";
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.forBlock['ruilong_RS485_FSFX_REQUESTFX'] = function() {
  var code = "rs485_fsfx.requestfx();\n";
  return code;
};
Blockly.Arduino.forBlock['ruilong_RS485_FSFX_READFX'] = function() {
  var code = "rs485_fsfx.readfx()";
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};
Blockly.Arduino.forBlock['ruilong_RS485_FSFX_READFX_DIR'] = function() {
  var code = "rs485_fsfx.readfx_dir("+ this.getFieldValue('DIR')+")";
  return [code, Blockly.Arduino.ORDER_ATOMIC];
};

})();